/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DownloadGui.javaS
 *
 * Created on 14/09/2010, 06:45:57 PM
 */
package download.manga;

import download.manga.hilos.HiloDescarga;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import org.jdesktop.swingx.util.OS;
import utils.servers.SubManga;

/**
 *
 * @author killan
 */
public class DownloadGui extends javax.swing.JFrame {

    static {
        Properties prop = new Properties();
        try {
            File f = new File("./propi/");
            if (!f.exists()) {
                f.mkdirs();
            }
            f = new File("./propi/pathProperties.properties");
            if (!f.exists()) {
                f.createNewFile();
            }
            String prp = "./propi/pathProperties.properties";
            prop.load(new FileInputStream(f));

            if (!Boolean.getBoolean(prop.getProperty("conf"))) {
                if (OS.isWindows()) {
                    prop.setProperty("so", "windows");
                    prop.setProperty("globPath", "C:/imanga/");
                    prop.setProperty("conf", "true");
                } else if (OS.isLinux()) {
                    prop.setProperty("so", "linux");
                    prop.setProperty("globPath", "/home/imanga/");
                    prop.setProperty("conf", "true");
                }

                prop.store(new FileOutputStream(prp), null);
            }
            System.out.println(prop.size());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
    private SubManga sm;
    private Image image;
    private String globPath = "/";
    private File f;
    private Properties prop = new Properties();
    private HiloDescarga hilo = new HiloDescarga();

    /** Creates new form UrlToImage */
    public DownloadGui() {
        super("Download Mangas");
        initComponents();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }

        f = new File("./propi/pathProperties.properties");
        try {
            prop.load(new FileInputStream(f));
        } catch (Exception e) {
            System.out.println(e);
        }

        globPath = prop.getProperty("globPath");
        f = new File(globPath + "Conf/series.konf");
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            jComboBox1.removeAllItems();
            while ((linea = br.readLine()) != null) {
                jComboBox1.addItem(linea);
            }
            br.close();
        } catch (Exception ex) {
        }
        jComboBox1.setSelectedIndex(0);
        try {
            f = new File(globPath + "Conf/MangaList/"
                    + jComboBox1.getSelectedItem().toString() + ".konf");
            if (!f.exists()) {
                sm.getChaptersList(globPath, jComboBox1.getSelectedItem().toString(), "completa");
                f.createNewFile();
            }
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            jComboBox2.removeAllItems();
            while ((linea = br.readLine()) != null) {
                int idx = linea.indexOf("/");
                jComboBox2.addItem(linea.substring(0, idx));
            }
            br.close();
        } catch (Exception e) {
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pagLabel = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        serieLabel = new javax.swing.JLabel();
        capituloLabel = new javax.swing.JLabel();
        capLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        updateMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pagLabel.setText("Paginas: ");

        jComboBox2.setMaximumSize(new java.awt.Dimension(166, 32767));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        jButton4.setText("Descargar Capítulo");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Descargar Manga");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        serieLabel.setText("Serie");

        capituloLabel.setText("Capítulo");

        capLabel.setText("Capítulo: ");

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        updateMenuItem.setText("Update All Series");
        updateMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(updateMenuItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox2, 0, 304, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, 304, Short.MAX_VALUE)
                    .addComponent(serieLabel)
                    .addComponent(capituloLabel)
                    .addComponent(pagLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5))
                    .addComponent(capLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(serieLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(capituloLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pagLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(capLabel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void DDdef() {
        /*
        JFileChooser fc = new JFileChooser();
        int dx = fc.showOpenDialog(this);
        File f2;
        if (dx == fc.APPROVE_OPTION) {
        f2 = fc.getSelectedFile();
        jLabel3.setText(f2.getAbsolutePath());
        }
         */
    }

    public Image getImagen(String url) {
        Image imagen;
        BufferedImage bufferedImage = null;
        //Realizamos la lectura del archivo.
        File archivo = new File(url);
        try {
            bufferedImage = (ImageIO.read(archivo));
        } catch (IOException ex) {
        }
        imagen = bufferedImage;
        return imagen;
    }//fin mètodo: getImagen()..

/*    public void setImageinImagePanel(String url, JXImagePanel panel) {
        //Inicializamos nuestra variable.
        JXImagePanel paneldeImagen = panel;
        Image imagen = null;
        paneldeImagen.setImage(null);
        //Obtenemos la imagen a partir de la url proporcionada.
        imagen = getImagen(url);
        //Obtenemos el ancho y alto del panel el caul no servira para redimen---
        //sionar a la imagen.
        int ancho = panel.getWidth();
        int alto = panel.getHeight();
        imagen = redimensionarImagen(imagen, ancho, alto);
        //Insertamos la imagen redimensionada en el panel.
        paneldeImagen.setImage(imagen);
    }//Fin metodo: setImageinImagePanel.
*/
    public Image redimensionarImagen(Image imagen, int ancho, int alto) {
        return imagen.getScaledInstance(ancho, alto, Image.SCALE_AREA_AVERAGING);
    }//FIN: redimensionarImagen.

    public void setJLabel3(String s) {
        pagLabel.setText(s);
    }

    public void setJC1() {
        f = new File(globPath + "Conf/series.konf");
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            jComboBox1.removeAllItems();
            while ((linea = br.readLine()) != null) {
                jComboBox1.addItem(linea);
            }
            br.close();
        } catch (Exception ex) {
        }
        jComboBox1.setSelectedIndex(0);
    }

    public void setJC2() {
        try {
            f = new File(globPath + "Conf/MangaList/"
                    + jComboBox1.getSelectedItem().toString() + ".konf");
            if (!f.exists()) {
                hilo.getChapterList(this);
            }
            BufferedReader br = new BufferedReader(new FileReader(f));
            String linea = "";
            jComboBox2.removeAllItems();
            while ((linea = br.readLine()) != null) {
                int idx = linea.indexOf("/");
                jComboBox2.addItem(linea.substring(0, idx));
            }
            br.close();
        } catch (Exception e) {
        }
    }

    public String getJCB1() {
        return jComboBox1.getSelectedItem().toString();
    }

    public int getJCB2Count() {
        return jComboBox2.getItemCount();
    }

    public JComboBox getAJCB2()
    {
        return jComboBox2;
    }

    public String getJCB2() {
        return jComboBox2.getSelectedItem().toString();
    }

    public void setCapLabel(String s){
        capLabel.setText(s);
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        hilo.getPages(this);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        hilo.getManga(this);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        hilo.getChapterList(this);
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
        // TODO add your handling code here:
        hilo.getNumberPages(this);
    }//GEN-LAST:event_jComboBox2ItemStateChanged

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        // TODO add your handling code here:
        hilo.getMangaList(this);
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void updateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateMenuItemActionPerformed
        // TODO add your handling code here:
        hilo.getMangaList(this);
        hilo.getChapterList(this);
    }//GEN-LAST:event_updateMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DownloadGui uTI = new DownloadGui();
                uTI.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel capLabel;
    private javax.swing.JLabel capituloLabel;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel pagLabel;
    private javax.swing.JLabel serieLabel;
    private javax.swing.JMenuItem updateMenuItem;
    // End of variables declaration//GEN-END:variables
}
